package com.yili.controller.api;

import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yili.controller.ApiResponse;
import com.yili.printer.service.PrintService;
import com.yili.printer.generator.Generator;
import com.yili.printer.generator.ProductLabelGenerator;

@RestController
@RequestMapping("/api")
public class ProductPrintController {

    private static final Logger logger = LogManager.getLogger(ProductPrintController.class);

    @Autowired
    private PrintService printService;

    /**
     * 产品标签打印接口
     * 
     * @param productName     产品名称
     * @param manufacturer    生产商
     * @param licenseNumber   生产许可证编号
     * @param postalCode      邮编
     * @param productionBatch 生产批次
     * @param bagCount        袋数
     * @param specification   规格
     * @return 打印结果
     */
    @PostMapping("/print-product")
    public ApiResponse<Map<String, Integer>> printProduct(
            @RequestParam String productName,
            @RequestParam String manufacturer,
            @RequestParam String licenseNumber,
            @RequestParam String postalCode,
            @RequestParam String productionBatch,
            @RequestParam String bagCount,
            @RequestParam String specification) {

        try {
            // 参数验证
            if (productName == null || productName.trim().isEmpty()) {
                return new ApiResponse<>(400, "产品名称不能为空", false, null);
            }
            if (manufacturer == null || manufacturer.trim().isEmpty()) {
                return new ApiResponse<>(400, "生产商不能为空", false, null);
            }
            if (productionBatch == null || productionBatch.trim().isEmpty()) {
                return new ApiResponse<>(400, "生产批次不能为空", false, null);
            }
            if (bagCount == null || bagCount.trim().isEmpty()) {
                return new ApiResponse<>(400, "袋数不能为空", false, null);
            }
            if (specification == null || specification.trim().isEmpty()) {
                return new ApiResponse<>(400, "规格不能为空", false, null);
            }

            // 创建打印数据Map
            Map<String, String> printData = new HashMap<>();
            printData.put("productName", productName.trim());
            printData.put("manufacturer", manufacturer.trim());
            printData.put("licenseNumber", licenseNumber != null ? licenseNumber.trim() : "");
            printData.put("postalCode", postalCode != null ? postalCode.trim() : "");
            printData.put("productionBatch", productionBatch.trim());
            printData.put("bagCount", bagCount.trim());
            printData.put("specification", specification.trim());

            // 执行打印操作
            try {
                Generator generator = new ProductLabelGenerator();
                printService.printLabel(printData, generator);
                
                // 打印成功
                Map<String, Integer> resultData = new HashMap<>();
                resultData.put("printCount", 1);
                resultData.put("totalCount", 1);
                
                return new ApiResponse<>(200, "产品标签打印成功", true, resultData);
                
            } catch (Exception e) {
                logger.error("打印产品标签失败，产品名称: {}, 生产商: {}, 生产批次: {}, 错误信息: {}",
                        productName, manufacturer, productionBatch, e.getMessage());
                return new ApiResponse<>(500, "打印失败，请检查打印机连接或稍后重试: " + e.getMessage(), false, null);
            }

        } catch (Exception e) {
            logger.error("产品标签打印过程中发生异常: {}", e.getMessage());
            return new ApiResponse<>(500, "打印过程中发生异常: " + e.getMessage(), false, null);
        }
    }
}